import wx


# 继承Frame
class Menu(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(Menu, self).__init__(*args, **kwargs)
        # 初始化窗口UI
        self.init_ui()

    def init_ui(self):
        # 创建菜单栏
        menu_bar = wx.MenuBar()
        # 新建"文件"菜单
        file_menu = wx.Menu()
        file_menu.Append(wx.ID_NEW, "新建文档")
        file_menu.Append(wx.ID_OPEN, "打开")
        file_menu.AppendSeparator()  # 添加分割线
        file_menu.Append(wx.ID_CLOSE, "关闭")
        # 通过ID绑定事件
        self.Bind(wx.EVT_MENU, self.close, id=wx.ID_CLOSE)
        self.Bind(wx.EVT_MENU, self.cl, id=wx.ID_NEW)
        file_menu.Append(wx.ID_SAVE, "保存")
        menu_bar.Append(file_menu, "文件")  # 添加到菜单栏

        # 新建"编辑"菜单
        edit_menu = wx.Menu()
        edit_menu.Append(wx.ID_COPY, "复制")
        edit_menu.Append(wx.ID_PASTE, "粘贴")
        edit_menu.AppendSeparator()  # 添加分割线
        find = wx.Menu()  # 查找子菜单
        find.Append(wx.ID_FIND, "查找...")
        find.Append(wx.ID_ANY, "查找上一个...")
        find.Append(wx.ID_ANY, "查找下一个...")
        edit_menu.AppendSubMenu(find, "查找")
        menu_bar.Append(edit_menu, "编辑")  # 添加到菜单栏

        # 新建"视图"菜单
        view_menu = wx.Menu()
        page = view_menu.Append(wx.ID_VIEW_LIST, "视图页面",
                                kind=wx.ITEM_CHECK)
        view_menu.Check(page.GetId(), True)  # 默认勾选
        self.Bind(wx.EVT_MENU, self.show_page, page)
        view_menu.Append(wx.ID_ANY, "功能区", kind=wx.ITEM_CHECK)
        menu_bar.Append(view_menu, "视图")  # 添加到菜单栏


        # 添加到Frame中
        self.SetMenuBar(menu_bar)
        self.SetSize((900, 500))
        self.SetTitle("实现菜单栏功能")
        self.Centre()
        self.Show(True)

    def close(self, e):
        print("关闭文档")
        self.Close()
    def cl(self,e):
        wx.MessageBox("点击")
    def show_page(self, e):
        check = e.IsChecked()
        print(check)


def main():
    app = wx.App(False)
    Menu(None)
    app.MainLoop()


if __name__ == '__main__':
    main()